<?php

//个人主页控制器
class Home extends Controller {

	var $userid;
	var $isowner = false;
	
	function Home()
	{
		parent::Controller();	
		$this->load->model("accounts_model", "am");
		$this->load->model("sitestatis_model", "ssm");
		$this->load->model("forum_model");
		$this->load->model("message_model");
		$this->load->model("comment_model");
		
		//登陆状态检测
		$this->userid = $this->am->is_logined();
		$this->account = $this->am->get_account();
		
		//设置此页面的过期 
		header("Expires: Mon, 26 Jul 1970 05:00:00 GMT");      
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");    
		header("Cache-Control: no-cache, must-revalidate");   
		header("Pragma: no-cache"); 
	}
	
	//个人主页默认首页
	function index()
	{
		//参数处理
		parse_str($_SERVER['QUERY_STRING'], $this->param);
		if(!empty($this->param) && !empty($this->param['action']) && !empty($this->param['userid'])) {
			$action = $this->param['action'];
			$this->account_id = $this->param['userid'];
		} else {
			$action = u(2);
			$this->account_id = u(3);
		}
		if(!$action || !$this->account_id) {
//			show_failed("非法请求", "/");
			redirect("/");
		}
		//获取帐户信息
		$this->viewdata["userinfo"] = $this->am->get($this->account_id);
		
		//输出公共header
		$pagedata["topmenuid"] = 3;
		if(!isset($pagedata["title"])) $pagedata["title"] = $this->viewdata["userinfo"]["username"]." | 热闹网";
		if(!isset($pagedata["keywords"])) $pagedata["keywords"] = "热闹、renao、热闹网、renao100、热闹100、轻站、轻、网站、轻型网站、维基、小站、兴趣、专页、博客、社区、空间、互动";
		if(!isset($pagedata["description"])) $pagedata["description"] = "热闹网(renao100.com) - 让每个人拥有史上最灵活的兴趣空间。为自己喜欢的事物建立轻型网站，并允许维基创建内容。";
		$this->autoload_model->output_header($pagedata);
		$this->viewdata["account"] = $this->account;

		//检测是否看自己
		if($this->userid == $this->account_id) $this->isowner = true;
		$this->viewdata["isowner"] = $this->isowner;
		($this->isowner) ? $this->viewdata["whoiam"] = "我" : $this->viewdata["whoiam"] = "他";
		switch($action) {
			case "account"://我的好站
				$this->__profile();
			break;	
			case "topic"://我的话题
				$this->__topic();
			break;	
			case "deltopic"://删除话题
				$this->__deltopic();
			break;
			case "message"://发信息
				$this->__message();
			break;
			case "show"://查看信息
				$this->__show();
			break;
			case "delete"://删除信息
				$this->__delete();
			break;
			case "deleteall"://删除所有信息
				$this->__deleteall();
			break;
			case "deleteallcomment"://删除所有信息
				$this->__deleteallcomment();
			break;
			case "deleteallout"://删除所有信息
				$this->__deleteall("outbox");
			break;
			case "deleteallno"://删除所有信息
				$this->__deleteall("notice");
			break;
			case "delselected"://删除信息
				$this->__delselected();
			break;
			case "delselectedcomment"://删除信息
				$this->__delselectedcomment();
			break;
			case "delselectedout"://删除信息
				$this->__delselected("outbox");
			break;
			case "delselectedno"://删除信息
				$this->__delselected("notice");
			break;
			case "showout"://查看发件信息
				$this->__show("outbox");
			break;
			case "inbox"://我的邮箱
				$this->__inbox();
			break;
			case "outbox"://发件箱
				$this->__outbox();
			break;
			case "notice"://通知
				$this->__notice();
			break;
			case "right"://公共右边
				$this->__right();
			break;
			case "comment"://回应我的
				$this->__comment();
			break;
			default://个人主页
				$this->__site();
		}
		//输出公共footer
		$this->autoload_model->output_footer();
	}
	
	//查看信息
	function __show($box = "inbox")
	{
		//检测安全
		if(!$this->isowner || !$messageid = u(4)) redirect("/");
		//获取信息
		$message = $this->message_model->show($this->account_id, $messageid, $box);
		//设置已读
		$this->message_model->setread($messageid, $box);
		$this->viewdata['message'] = $message;
		$this->load->view(__CLASS__."/show", $this->viewdata);
	}
	
	//删除信息
	function __delete()
	{
		if ($dosubmit = p("dosubmit")) {
			$messageid = p("messageid");
			$box = p("box");
			$result = $this->message_model->delete($messageid, $box);
			if($result) show_success("删除成功", "/home/".$box."/".$this->account_id);
		}
		redirect("/");
	}
	
	//删除选择
	function __delselected($box = "inbox")
	{
		if(!$delstr = u(4)) redirect("/");
		$result = $this->message_model->delselected($delstr, $box);
		if($result) show_success("删除成功", "/home/".$box."/".$this->account_id);
	}
	
	//删除选择
	function __delselectedcomment()
	{
		if(!$delstr = u(4)) redirect("/");
		$result = $this->comment_model->delselected_comment($delstr);
		if($result) show_success("删除成功", "/home/comment/".$this->account_id);
	}
	
	//删除所有
	function __deleteall($box = "inbox")
	{
		$result = $this->message_model->deleteall($this->account_id, $box);
		if($result) show_success("删除成功", "/home/".$box."/".$this->account_id);
	}
	
	//删除所有
	function __deleteallcomment()
	{
		$result = $this->comment_model->deleteall_comment($this->account_id);
		if($result) show_success("删除成功", "/home/comment/".$this->account_id);
	}
	
	//发信息
	function __message()
	{
		//检测安全
		if(!$this->isowner || !$toid = u(4)) redirect("/");
		//获取用户
		$touser = $this->am->get($toid);
		//提交表单
		if ($dosubmit = p("dosubmit")) {
			$title = p("title");
			$description = p("description");
			$result = $this->message_model->send($this->account_id, $toid, $title, $description);
			if ($result) {
				show_success("发送成功", "/home/outbox/".$this->account_id);
			} else {
				show_failed("发送失败");
			}
		}
		
		$this->viewdata['touser'] = $touser;
		$this->load->view(__CLASS__."/message", $this->viewdata);
	}
	
	//回应我的
	function __comment()
	{
		//检测安全
		if(!$this->isowner) redirect("/");

		//跳转链接处理
		if (!empty($this->param['link']) && !empty($this->param['id'])) {
			//设置已读
			$this->comment_model->setread($this->param['id']);
			redirect($this->param['link']);
		}		
		
		//获取数据
		$per_page = 20;
		$offset = u(4);
		$lists = $this->comment_model->lists_comment($this->account_id, $per_page, $offset);
		//分页处理
		$base_url = gurl("home/comment/".$this->account_id);
		$total_rows = $this->comment_model->count_list_comment($this->account_id);
		$pagestring = pages($base_url,$total_rows,4,$per_page);
		$this->viewdata["offset"] = $offset;
		$this->viewdata["pagestring"] = $pagestring;
		
		$this->viewdata["lists"] = $lists;
		$this->load->view(__CLASS__."/comment", $this->viewdata);
	}
	
	//收件箱
	function __inbox()
	{
		//检测安全
		if(!$this->isowner) redirect("/");
		
		//获取数据
		$per_page = 20;
		$offset = u(4);
		$lists = $this->message_model->lists_inbox($this->account_id, $per_page, $offset);
		
		//分页处理
		$base_url = gurl("home/inbox/".$this->account_id);
		$total_rows = $this->message_model->count_list_inbox($this->account_id);
		$pagestring = pages($base_url,$total_rows,4,$per_page);
		$this->viewdata["offset"] = $offset;
		$this->viewdata["pagestring"] = $pagestring;
		
		$this->viewdata["lists"] = $lists;
		$this->load->view(__CLASS__."/inbox", $this->viewdata);
	}
	
	//发件箱
	function __outbox()
	{
		//检测安全
		if(!$this->isowner) redirect("/");
		
		//获取数据
		$per_page = 20;
		$offset = u(4);
		$lists = $this->message_model->lists_outbox($this->account_id, $per_page, $offset);
		
		//分页处理
		$base_url = gurl("home/outbox/".$this->account_id);
		$total_rows = $this->message_model->count_list_outbox($this->account_id);
		$pagestring = pages($base_url,$total_rows,4,$per_page);
		$this->viewdata["offset"] = $offset;
		$this->viewdata["pagestring"] = $pagestring;
		
		$this->viewdata["lists"] = $lists;
		$this->load->view(__CLASS__."/outbox", $this->viewdata);
	}
	
	//通知
	function __notice()
	{
		//检测安全
		if(!$this->isowner) redirect("/");
		
		//设置已读
		$this->message_model->setread_notice($this->account_id);
		
		//获取数据
		$per_page = 20;
		$offset = u(4);
		$lists = $this->message_model->lists_notice($this->account_id, $per_page, $offset);
		
		//获取小站信息
		$usersite = $this->sm->get_usersite($this->account_id);
		
		//分页处理
		$base_url = gurl("home/notice/".$this->account_id);
		$total_rows = $this->message_model->count_list_notice($this->account_id);
		$pagestring = pages($base_url,$total_rows,4,$per_page);
		$this->viewdata["offset"] = $offset;
		$this->viewdata["pagestring"] = $pagestring;
		$this->viewdata["lists"] = $lists;
		$this->viewdata["usersite"] = $usersite;
		$this->load->view(__CLASS__."/notice", $this->viewdata);
	}
	
	//我的话题
	function __topic()
	{
		$offset = u(4);
		$per_page = 20;
		
		//话题列表
		$lists = $this->forum_model->lists_user($this->account_id, $per_page, $offset);
		
		//分页处理
		$base_url = '/home/topic/'.$this->account_id;
		$total_rows = $this->forum_model->count_lists_user($this->account_id);
		$pagestring = pages($base_url,$total_rows,4,$per_page);
		$this->viewdata["offset"] = $offset;
		$this->viewdata["pagestring"] = $pagestring;
		
		$this->viewdata["lists"] = $lists;
		$this->load->view(__CLASS__."/topic", $this->viewdata);
	}
	
	//删除话题
	function __deltopic()
	{
		$this->load->model("forum_model", "fm");
		$id = u(4);
		if(!$id) {
			redirect("/");
		}
		$result = $this->fm->delete($id);
		if($result) show_success("删除成功");
	}
	
	//我的好站
	function __site()
	{
		$offset = u(4);
		$per_page = 80;
		
		//获取小站信息
		$usersite = $this->sm->get_usersite($this->account_id);
		
		//获取我关注的小站
		$sites = $this->sm->get_fansite($this->account_id, $per_page, $offset);
		
		//分页处理
		$base_url = '/home/site/'.$this->account_id;
		$total_rows = $this->ssm->get_userfuns($this->account_id);
		$pagestring = pages($base_url,$total_rows,4,$per_page);
		$this->viewdata["offset"] = $offset;
		$this->viewdata["pagestring"] = $pagestring;
		
		$this->viewdata["usersite"] = $usersite;
		$this->viewdata["sites"] = $sites;
		$this->load->view(__CLASS__."/site", $this->viewdata);
	}
	
	//右边区域
	function __right()
	{
		ob_start();
		//赞美数据
		$this->load->model("digg_model");
		$this->viewdata["digg"] = $this->digg_model->get($this->account_id);
		$this->viewdata["diggcount"] = $this->digg_model->get_count($this->account_id);
		$this->load->view(__CLASS__."/right", $this->viewdata);
		$contents = jsoutputformat(ob_get_contents());//js输出转换
		ob_end_clean();
		echo "document.write('$contents');";exit;
	}
	
	//个人主页
	function __profile()
	{
		$this->load->model("tag_model");
		
		//================= LOGO图片处理 ============/
		//加载类库
		$this->load->library("jqphoto");
		$this->jqphoto->thumb_width = "180";
		$this->jqphoto->thumb_height = "180";
		
		//1) 表单1：大图片上传处理
		if($dosubmit = p("dosubmit") && $_FILES['image']['name'] != "") {
			$this->jqphoto->upload($_FILES['image']);
		}
		//2) 表单2：裁切图处理
		if($dosubmit2 = p("dosubmit2")) {
			$this->jqphoto->upload_thumb($_POST);
		}
		//3) 获取数据
		$current_large_image_width = $current_large_image_height = "";
		if($this->jqphoto->large_photo_exists != "") {
			$current_large_image_width = $this->jqphoto->getWidth($this->jqphoto->large_image_location);
			$current_large_image_height = $this->jqphoto->getHeight($this->jqphoto->large_image_location);
		}
		$this->viewdata["large_photo_exists"] = $this->jqphoto->large_photo_exists;
		$this->viewdata["thumb_photo_exists"] = $this->jqphoto->thumb_photo_exists;
		$this->viewdata["upload_path"] = $this->jqphoto->upload_path;
		$this->viewdata["large_image_name"] = $this->jqphoto->large_image_name;
		$this->viewdata["thumb_width"] = "180";
		$this->viewdata["thumb_height"] = "180";
		$this->viewdata["current_large_image_width"] = $current_large_image_width;
		$this->viewdata["current_large_image_height"] = $current_large_image_height;
		//================= LOGO图片处理结束 ============/
		
		//用户标签
		$usertags = $this->tag_model->get_usertags($this->account_id);
		$tagslist = array();
		foreach ($usertags as $row) {
			$tagslist[] = $row["tagid"];
		}
		
		//标签信息列表
		$tags = $this->tag_model->lists();
		$this->viewdata["tags"] = $tags;
		$this->viewdata["tagslist"] = $tagslist;
		$this->viewdata["usertags"] = $usertags;
		$this->load->view(__CLASS__."/index", $this->viewdata);
	}
	
}

/* End of file */